package org.scut.smartgeometry.act;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import org.scut.smartgeometry.R;
import org.scut.smartgeometry.act.fragment.MainMenuFragment;
import org.scut.smartgeometry.act.fragment.PaintFragment;
import org.scut.smartgeometry.utils.ConstantsSG;

import java.io.FileNotFoundException;

public class HomePageAct extends FragmentActivity {



    private FragmentManager fragmentManager;
    private MainMenuFragment fragment_main_menu;
    private PaintFragment fragment_paint;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        fragmentManager = getSupportFragmentManager();

        FragmentTransaction ft = fragmentManager.beginTransaction();

        fragment_main_menu = new MainMenuFragment(this);
        ft.add(R.id.container_main_menu, fragment_main_menu);

        fragment_paint = new PaintFragment(this);
        ft.add(R.id.container_paint, fragment_paint);

        ft.commit();

//        fragment_main_menu = (Fragment) findViewById(R.id.main_menu_fragment);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_page, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.action_settings:

                break;
            default:
                break;
        }

        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(R.animator.move_in, R.animator.move_out);

        if(fragment_main_menu.isHidden()){
            ft.show(fragment_main_menu);
        }
        else{
            ft.hide(fragment_main_menu);
        }
        ft.commit();

        return super.onOptionsItemSelected(item);
    }


    public void clickMenu(String btn_name){
        fragment_paint.clickMenu(btn_name);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case ConstantsSG.REQUEST_CODE_FOR_SELECT_BAC:
                if(resultCode == RESULT_OK){
                    Uri uri = data.getData();
                    ContentResolver cr = this.getContentResolver();
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                        fragment_paint.getHandwriteView().selectBacForResult(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
